package com.cqeec.demo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cqeec.demo.bean.AdminRole;
import com.cqeec.demo.mapper.AdminRoleMapper;
import com.cqeec.demo.service.AdminRoleService;

/**
 * 系统用户角色业务逻辑接口实现类
 * 
 * @Title: AdminAuthServiceImpl
 * @Descripton: 定义系统用户角色业务逻辑接口实现
 */
@Service("adminRoleService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class })
public class AdminRoleServiceImpl implements AdminRoleService {
	@Resource
	private AdminRoleMapper adminRoleMapper;

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<AdminRole> getUserRole(Long userid) {
		return adminRoleMapper.getUserRole(userid);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Map<String, Object> getRoleListByPage(String rolename, int page, int pagesize) {
		Map<String, Object> params = new HashMap<>();
		params.put("rolename", rolename);
		int counts = adminRoleMapper.recordCount(params);
		int pages = counts % pagesize == 0 ? counts / pagesize : counts / pagesize + 1;
		if (page >= pages) {
			page = pages;
		}
		if (page <= 1) {
			page = 1;
		}
		params.put("page", (page - 1) * pagesize);
		params.put("pagesize", pagesize);
		List<AdminRole> role_list = adminRoleMapper.getRoleListByPage(params);
		Map<String, Object> result = new HashMap<>();
		result.put("status", true);
		result.put("message", "");
		result.put("total", counts);
		result.put("rows", role_list);
		return result;
	}

	@Override
	public AdminRole findEntityByName(String role_name) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Map<String, Object> saveEntity(AdminRole adminRole) {
		// TODO Auto-generated method stub
		adminRoleMapper.saveEntity(adminRole);
		Map<String, Object> result = new HashMap<>();
		result.put("status", true);
		result.put("message", "操作成功");
		return result;
	}
	
	@Override
	public Map<String, Object> updateEntity(AdminRole adminRole) {
		adminRoleMapper.updateEntity(adminRole);
		Map<String, Object> result = new HashMap<>();
		result.put("status", true);
		result.put("message", "操作成功");
		return result;
	}

	@Override
	public Map<String, Object> doDisAuth(AdminRole adminRole) {
		// TODO Auto-generated method stub
		adminRoleMapper.doDisAuth(adminRole);
		Map<String, Object> result = new HashMap<>();
		result.put("status", true);
		result.put("message", "操作成功");
		return result;
	}
}
